const fp = require('loadsh/fp')
const cars = [
	{
		name: 'Ferrari FF',
		horsepower: 600,
		dollar_value: 700000,
		in_stock: true,
	},
	{
		name: 'Ferrari FF1',
		horsepower: 650,
		dollar_value: 64899,
		in_stock: false,
	},
	{
		name: 'Ferrari FF2',
		horsepower: 550,
		dollar_value: 703000,
		in_stock: false,
	},
	{
		name: 'Ferrari FF3',
		horsepower: 600,
		dollar_value: 1700000,
		in_stock: true,
	},
	{
		name: 'Ferrari FF4',
		horsepower: 543,
		dollar_value: 114200,
		in_stock: true,
	},
	{
		name: 'Ferrari FF5',
		horsepower: 603,
		dollar_value: 70000,
		in_stock: false,
	},
]
//1 使用函数组合fp.flowRight()
let getLastInStock = fp.flowRight(fp.prop('in_stock'), fp.last)
console.log(getLastInStock(cars))
//2 使用函数组合fp.flowRight()
let getfirstName = fp.flowRight(fp.prop('name'), fp.first)
console.log(getfirstName(cars))
//3
let _average = function (xs) {
	return fp.reduce(fp.add, 0, xs) / xs.length
}
let averageDollarValue = fp.flowRight(
	_average,
	fp.map((car) => car.dollar_value)
)
console.log(averageDollarValue(cars))
//4 返回一个下滑线连接的小写字符串，把数组中的name转换为 ["Hello World"] =>["hello_world"]
// 0 map 遍历每个元素 1 空格替换，2 变成小写，
let _underscore = fp.replace(/\W+/g, '_')
let str_arr = ['Hello World']
let getValue = fp.flowRight(fp.map(fp.flowRight(fp.toLower, _underscore)))
console.log(getValue(str_arr))
